import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';

import 'rxjs/add/operator/map';

import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';

declare const $: any;
import { DxDataGridComponent } from '../../../../../node_modules/devextreme-angular';
import { ScheduleHistory } from 'app/ptpmc/model/report/schedule_history';

@Component({
  selector: 'app-config-email',
  templateUrl: './config-email.component.html',
  styleUrls: ['./config-email.component.scss'],
})

export class ConfigEmailComponent implements OnInit, AfterViewInit {

  @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
  /**查询按类型 */
  public typeItems = [{ id: 1, text: '按值统计' }, { id: 2, text: '按班次统计' }];
  public typeSelected = this.typeItems[0];

  /**选择项 */
  public items: any[] = [];
  public selectedItems: ScheduleHistory[] = [];


  /*生产线 - 班次*/
  public subAreaItems: SubArea[];

  public shiftItems: any[];
  public subAareSelected: any;

  public moduleItems = [{ text: 'WorkingPace'}];
  public moduleSelected: any;

  public userList: any[];

  filterVlaueItems: string[] = ['All'];
  addFilterVlaueItemItem(data) {
    const newItem = data.text;
    this.filterVlaueItems.push(newItem);
    return newItem;
  }

  filterChange(data) {
    if (data.value === 'All') {
      this.dataGrid.instance.clearFilter();
    } else {
      this.dataGrid.instance.filter(["duration", ">", data.value]);
    }
  }

  constructor(private commServices: CommService) {
  }
  ngOnInit() {
    this.getAllUser();
    this.shiftItems = this.commServices.shifts;
    this.subAreaItems = this.commServices.getSubAreas();
    this.subAareSelected = this.subAreaItems[0];
  }

  ngAfterViewInit() {
  }

  getAllUser() {
    this.commServices
      .getList({}, ReportUrls.AllUser)
      .subscribe(model => {
        if (model.resultCode === 200) {
          debugger
          this.userList = model.data.allUser;
        } else {
          console.log(model.resultCode + ',' + model.resultMessage);
        }
      });
  }

  userIsChecked(user) {
    if (this.items === undefined || this.items.length === 0) {
      return false;
    }
    return this.items.find(e => e.sendto === user.email) !== undefined;
  }

  addEmailUser(user) {
    let us: any = {
      module: this.moduleSelected.text, sendto: user.email, subArea: this.subAareSelected.subArea
    };

    if (this.userIsChecked(user)) {
      const u = this.items.find(e => e.sendto === user.email);
      us = {
        module: this.moduleSelected.text, sendto: user.email, subArea: this.subAareSelected.subArea, emailId: u.emailId
      };
    }
    this.commServices.getList(us, ReportUrls.AddConfigEmailUrl)
      .subscribe(model => {
        if (model.resultCode === 200) {
          this.commServices.showNotification('操作成功', 'info');
          this.getReportData();
        } else {
          alert('操作失败！' + model.resultMessage);
        }
      });
  }

  /**获取表格数据*/
  getReportData(): void {
    const queryModel: QueryModel = new QueryModel();
    queryModel.querySubAreaIds = [this.subAareSelected.subArea];
    queryModel.tag = this.moduleSelected.text;
    const url: string = ReportUrls.ConfigEmailUrl;
    debugger
    this.commServices
      .getList(queryModel, url)
      .subscribe(model => {
        this.items = [];
        if (model.resultCode === 200) {
          this.items = model.data;
          debugger
        } else {
          console.log(model.resultCode + ',' + model.resultMessage);
        }
      });
  }
  public updateFilterList: ScheduleHistory[] = [];
  onCheckBoxChange(entity: ScheduleHistory): void {
    let index: number = this.items.findIndex(e => e.subArea === entity.subArea && e.productionDate === entity.productionDate
      && e.shift === entity.shift);
    this.items
    if (index != -1) {
      this.items[index].reportFilter = entity.reportFilter === 1 ? 0 : 1;
      let upIndex: number = this.updateFilterList.findIndex(e => e.subArea === entity.subArea && e.productionDate === entity.productionDate
        && e.shift === entity.shift);
      if (upIndex != -1) {
        this.updateFilterList[upIndex] = this.items[index];
      }
      else {
        this.updateFilterList.push(this.items[index]);
      }
    }
  }
  saveChanges() {
  }
  /**获取TimeRange的值*/
  getTime(id: any): String {
    const timerange: String = $('#' + id).val();
    return timerange;
  }

}
